package com.tps.config;

import com.tps.service.security.SecurityUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author charlee.wu
 * @date 2018/5/8 15:34
 **/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private AuthenticationManager authenticationManager;

    private RedisConnectionFactory redisConnectionFactory;

    private SecurityUserDetailService securityUserDetailService;

    @Value("${server.port}")
    private String port;

    @Autowired
    public AuthorizationServerConfig(AuthenticationManager authenticationManager,
                                     RedisConnectionFactory redisConnectionFactory,
                                     SecurityUserDetailService securityUserDetailService) {
        this.authenticationManager = authenticationManager;
        this.redisConnectionFactory = redisConnectionFactory;
        this.securityUserDetailService = securityUserDetailService;
    }

    @Bean
    public RedisTokenStore redisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .allowFormAuthenticationForClients()
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("tps-erp")
                .secret("tps-erp")
                .redirectUris("http://localhost:" + port + "/callback")
                .authorizedGrantTypes("password", "authorization_code",
                        "client_credentials", "refresh_token", "implicit")
                .scopes("all")
                .resourceIds("oauth2-resource")
                .accessTokenValiditySeconds(7 * 24 * 60 * 60)
                .refreshTokenValiditySeconds(30 * 24 * 60 * 60);

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .userDetailsService(securityUserDetailService)
                .authenticationManager(authenticationManager)
                .tokenStore(redisTokenStore());
    }
}
